package top100.hash;

import java.util.*;

/**
 * @Author ZhangCuirong
 * @Date 2025/6/12 14:17
 * @description: 给你一个字符串数组，请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
 * <p>
 * 字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
 * <p>
 * <p>
 * <p>
 * 示例 1:
 * <p>
 * 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
 * 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
 * 示例 2:
 * <p>
 * 输入: strs = [""]
 * 输出: [[""]]
 * 示例 3:
 * <p>
 * 输入: strs = ["a"]
 * 输出: [["a"]]
 * <p>
 * <p>
 * 提示：
 * <p>
 * 1 <= strs.length <= 104
 * 0 <= strs[i].length <= 100
 * strs[i] 仅包含小写字母
 */
public class Solution49 {
    //[["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"],["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]]
    //[["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]]
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<Long, List<String>> temp = new HashMap<>();
        for (String str : strs) {
            long hashCode = 0;
            char[] s = str.toCharArray();
            Arrays.sort(s);
            for (int i = 0; i < s.length; i++) {
                hashCode = hashCode * 27 + (s[i] - 'a' + 1);
            }
            if (temp.containsKey(hashCode)) {
                temp.get(hashCode).add(str);
            } else {
                temp.put(hashCode, new ArrayList<>());
                temp.get(hashCode).add(str);
            }
        }
        return new ArrayList<>(temp.values());
    }

    public static void main(String[] args) {
        String[] strs = {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
        System.out.println(new Solution49().groupAnagrams(strs));
    }
}
